﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace DAO
{
    public class DataProvider
    {
        private static string _ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + @"..\..\..\DuLieu\" + "Database.accdb";
        

        private static OleDbConnection _Connection = null;
        private static OleDbCommand _Command = null;

        public static void OpenConnection()
        {
            _Connection = new OleDbConnection(_ConnectionString);
            _Connection.Open();
        }

        public static void CloseConnection()
        {
            _Connection.Close();
            _Connection = null;
        }

        public static void ExecuteNonQuery(string query)
        {
            if (_Connection != null)
            {
                try
                {
                    _Command = new OleDbCommand(query, _Connection);
                    _Command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static OleDbDataReader ExecuteReader(string query)
        {
            if (_Connection != null)
            {
                try
                {
                    _Command = new OleDbCommand(query, _Connection);
                    return _Command.ExecuteReader();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            return null;
        }
    }
}
